<?php

namespace App\Http\Controllers\WeChat;

use App\Http\Controllers\Controller;
use App\Models\WeChatKeyWord;
use App\Models\WeChatRule;
use App\Models\WeChatText;
use App\Services\WeChatService;
use Illuminate\Http\Request;

class TextController extends Controller
{
    //
    public function index()
    {
        $texts = WeChatText::where('site_id', site()['id'])->where('module_id', module()['id'])->get();

        return view('wechat.text.index', compact('texts'));

    }

    public function create()
    {
        return view('wechat.text.create');
    }

//路由参数会根据变量名称，来依赖注入类型
    public function edit(WeChatText $text)
    {

        return view('wechat.text.edit', compact('text'));
    }

    public function show(WeChatText $text)
    {

        return ['text' => $text];
    }

    public function update(Request $request, WeChatText $text, WeChatService $weChatService)
    {

        $rule = $weChatService->save('text');
        $text->contents = json_decode($request->input('wechat.contents'), true);
        $text->save();
        return back()->with('success', '文本回复保存成功');
    }

    public function store(Request $request, WeChatService $weChatService, WeChatText $weChatText)
    {

        $rule = $weChatService->save('text');

        $weChatText->create([
            'site_id' => site()['id'],
            'module_id' => module()['id'],
            'rule_id' => $rule['id'],
            'type' => $rule['type'],
            'contents' => json_decode($request->input('wechat.contents'), true)
        ]);
        return redirect()->route('wechat.text.index')->with('success', '文本回复添加成功');
    }



}










